const app = getApp()
const state = {
  a: [
    {
      name: "消炎药",
      tip: "药品种类",
      price: 50,
      id: 1
    },
    {
      name: "化疗药",
      tip: "药品种类",
      price: 200,
      id: 2
    }
  ],
  b: [
    {
      name: "心电图",
      tip: "项目种类",
      price: 50,
      id: 3
    },
    {
      name: "B超",
      tip: "项目种类",
      price: 80,
      id: 4
    }
  ]
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: "",
    pageHeight: "0px",
    activeTab: "a",
    loading: false,
    list: [],
    words: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.init()
  },

  onTabChange(e) {
    this.setData({
      activeTab: e.detail.name
    })
    this.init(this.data.searchValue)
  },

  
  onSearchChange(e) {
    this.data.searchValue = e.detail.value
  },

  onSearch(e){
    if(!this.data.searchValue) {
      wx.showToast({
        title: '请输入搜索关键字！',
        icon: "none"
      })
      return
    }
    this.setData({
      searchValue: this.data.searchValue,
      words: this.data.searchValue,
    })
    this.init(this.data.searchValue)
  },

  onCancel(e) {
    this.setData({
      searchValue: "",
      words: "",
    })
    this.init()
  },

  init(val) {
    this.getList(val)
  },

  getList() {
    this.setData({
      loading: true,
      list: []
    },() => {
      setTimeout(_=>{
        let list = state[this.data.activeTab]
        const val = this.data.words
        list = val ? list.filter(item => item.name.includes(val)) : []
        this.setData({
          loading: false,
          list
        })
      },300)
    })
  },

  getInfo(e) {
    this.setData({
      pageHeight: e.detail.bottom + "px"
    })
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    app.ws.setBadge()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})